//
//  SIXBasicTableViewController.m
//  weibo
//
//  Created by   李小云 on 16/5/21.
//  Copyright © 2016年 chuhan. All rights reserved.
//

#import "SIXBasicTableViewController.h"
#import "MYTimeField.h"
#import "SIXBasicField.h"
#import "MYCityField.h"


@interface SIXBasicTableViewController ()<UIAlertViewDelegate,UITextFieldDelegate,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UILabel *sexLable;
@property (weak, nonatomic) IBOutlet UILabel *emailLable;
@property (weak, nonatomic) IBOutlet UILabel *blogLable;
@property (weak, nonatomic) IBOutlet UILabel *qqLable;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;

@property (weak, nonatomic) IBOutlet MYCityField *placeField;
@property (weak, nonatomic) IBOutlet MYTimeField *dataField;

@property(nonatomic,assign)float keyboardHeight;

@property(nonatomic,assign)NSTimeInterval animationDuration;
@end

@implementation SIXBasicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(-30, 0, 0, 0);
    //监听键盘的弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
//取出系统键盘的高度 没用
-(void)KeyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    
    //获取高度
    NSValue *value = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];//关键的一句，网上关于获取键盘高度的解决办法，多到这句就over了。系统宏定义的UIKeyboardBoundsUserInfoKey等测试都不能获取正确的值。不知道为什么。。。
    CGSize keyboardSize = [value CGRectValue].size;
    float keyboardHeight = keyboardSize.height;
    _keyboardHeight = keyboardHeight;
    
    // 获取键盘弹出的时间
    NSValue *animationDurationValue = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    _animationDuration = animationDuration;
    //自定义的frame大小的改变的语句
}
//提供内方法 用于外界创建storyboard创建的静态tableview
+(instancetype)showChild{
    
    UIStoryboard *storyboard =[UIStoryboard storyboardWithName:@"BasicView" bundle:nil];
    SIXBasicTableViewController *basicVC = [storyboard instantiateInitialViewController];
    return basicVC;
}

//cell的点击事件监听
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取到对于indexPath的cell
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    //判断cell的子控件是否有button
    for (UIButton *view in cell.contentView.subviews) {
        
        if ([view isKindOfClass: [UIButton class]]) {
            
            NSString *str = [view titleForState:UIControlStateNormal];
            //如果有调用方法
            [self addAlert:str];
         }
    }
    //判断cell的子控件是否有UIlable 并且lable文字是否等于性别
    for (UILabel *lable in cell.contentView.subviews) {
        
        if ([lable isKindOfClass:[UILabel class]]&&[lable.text isEqualToString:@"性别"] ) {
            [self addSexView];
    }
    }
    //判断cell的子控件是否有UITextField 并且lable文字是否等于性别
    for (UITextField *Field in cell.contentView.subviews) {
        
        if ([Field isKindOfClass:[UITextField class]] ) {
            
            [self addField:Field];
            
        }
    }
}
#pragma mark -所在地和生日展示
//因为cell与键盘都能够点击 所以当点击cell的时候 让键盘成为第一响应者
-(void)addField:(UITextField *)field{
    //判断 点击不同键盘的时候 让不同的键盘成为第一响应者
    if ([field isKindOfClass:[MYTimeField class]]) {
        [self.dataField becomeFirstResponder];
    }
    
    if ([field isKindOfClass:[MYCityField class]]) {
        [self.placeField becomeFirstResponder];
    }
}
#pragma mark --性别cell展示
//当点击更改性别的时候调用
-(void)addSexView{
    //创建sheet视图
     UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"性别修改" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男", @"女", nil];
    [myActionSheet showInView:self.view];
}
//监听UIActionSheet的点击事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    _sexLable.text = [actionSheet buttonTitleAtIndex:buttonIndex];
}
#pragma mark --昵称 微博 博客 QQ cell展示
//点击昵称 微博 博客 QQ会调用
-(void)addAlert:(NSString *)str{
    //根据不同显示需求 拼接字符串
    static NSString *title = nil;
    if ([str isEqualToString:@"邮箱"] || [str isEqualToString:@"博客"]) {
        title = [NSString stringWithFormat:@"编辑  %@",str];
    }else{
        
        title = [NSString stringWithFormat:@"编辑%@",str];
    }
    NSString *cancelButtonTitle = @"取消";
    NSString *otherButtonTitle = @"确认";
    //创建UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    //创建UIAlertController的确认和取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       
    }];
    //设置键盘的占位字符串
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        
             textField.delegate = self;
            if ([str isEqualToString:@"邮箱"]) {
   
            textField.placeholder = @"例如:klaus@sina.com";
            }
            if ([str isEqualToString:@"博客"]) {
                
                textField.placeholder = @"例如:http://blog.sina.com.cn/sina";
            }
            if ([str isEqualToString:@"QQ"]) {
                
                textField.placeholder = @"例如:446054204";
            }
            if ([str isEqualToString:@"昵称"]) {
                
                textField.placeholder = @"木子小楼";
            }
     }];
    //添加确认和取消
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    //把UIAlertController modal上来
    [self presentViewController:alertController animated:YES completion:nil];
}
//根据占位符不同 判断当用户点击的时候 把用户输入的字符串赋值给不同的Lable
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length != 0 &&[textField.placeholder isEqualToString:@"例如:klaus@sina.com"]) {
        NSString *str = textField.text;
        _emailLable.text = str;
    }
    if (textField.text.length != 0 &&[textField.placeholder isEqualToString:@"例如:http://blog.sina.com.cn/sina"]) {
        NSString *str = textField.text;
        _blogLable.text = str;
    }
    if (textField.text.length != 0 &&[textField.placeholder isEqualToString:@"例如:446054204"]) {
        NSString *str = textField.text;
        _qqLable.text = str;
    }
    if (textField.text.length != 0&&[textField.placeholder isEqualToString: @"木子小楼"]) {
        NSString *str = textField.text;
        _nameLable.text = str;
    }
}
@end
